# Load Necessary Packages
library(rdrobust)
library(tidyr)
library(dplyr)
library(foreign)
library(margins)
library(interplot)
library(ggpubr)
library(haven)
library(stargazer)
library(rddensity)
library(fixest)

# Load Data
setwd("/users/josephphillips/Dropbox/Projects/Magnanimity of Winners")
## ANES
data <- read.csv("anes.csv",header=T,sep=",")
anes20 <- read.dta("anes20.dta")
## House
house <- read.csv("1976-2022-house.csv",header=T,sep=",")
## Senate
senate <- read.csv("1976-2020-senate.csv",header=T,sep=",")
## President
president <- read.csv("1976-2020-president.csv",header=T,sep=",")

# Clean Data
## PID
data$pid <- ifelse(data$VCF0301<1,NA,data$VCF0301)
anes20$pid <- ifelse(anes20$V201231x<0,NA,anes20$V201231x)
## Affective Polarization
data$ft_dem <- ifelse(data$VCF0218>97,0,data$VCF0218)/97
data$ft_rep <- ifelse(data$VCF0224>97,0,data$VCF0224)/97
data$cses_dem <- ifelse(data$VCF9201<0,0,data$VCF9201)/10
data$cses_rep <- ifelse(data$VCF9202<0,0,data$VCF9202)/10
data$inpartyft <- ifelse(data$pid<4,data$ft_dem,ifelse(data$pid>4,data$ft_rep,NA))
data$outpartyft <- ifelse(data$pid<4,data$ft_rep,ifelse(data$pid>4,data$ft_dem,NA))
data$polarizft <- (((data$inpartyft-data$outpartyft)+1)/2)-0.5
data$inpartycses <- ifelse(data$pid<4,data$cses_dem,ifelse(data$pid>4,data$cses_rep,NA))
data$outpartycses <- ifelse(data$pid<4,data$cses_rep,ifelse(data$pid>4,data$cses_dem,NA))
data$polarizcses <- (((data$inpartycses-data$outpartycses)+1)/2)-0.5
### Use FT's if available post-election, CSES if the only one available post-election
data$V840004 <- ifelse(data$VCF0004==1984,data$VCF0006,NA) ## Create ID for merging in 1984 post-election values
data$inparty <- ifelse(data$VCF0004==1978 | data$VCF0004==1980 | data$VCF0004==1982 | data$VCF0004==1984 | data$VCF0004==1986 | data$VCF0004==1988 | data$VCF0004==1990 | data$VCF0004==1992 | data$VCF0004==1994 | data$VCF0004==1998 | data$VCF0004==2002,data$inpartyft,ifelse(data$VCF0004==1996 | data$VCF0004==2004 | data$VCF0004==2008 | data$VCF0004==2012 | data$VCF0004==2016 | data$VCF0004==2020,data$inpartycses,NA))
data$outparty <- ifelse(data$VCF0004==1978 | data$VCF0004==1980 | data$VCF0004==1982 | data$VCF0004==1984 | data$VCF0004==1986 | data$VCF0004==1988 | data$VCF0004==1990 | data$VCF0004==1992 | data$VCF0004==1994 | data$VCF0004==1998 | data$VCF0004==2002,data$outpartyft,ifelse(data$VCF0004==1996 | data$VCF0004==2004 | data$VCF0004==2008 | data$VCF0004==2012 | data$VCF0004==2016 | data$VCF0004==2020,data$outpartycses,NA))
data$polariz <- (((data$inparty-data$outparty)+1)/2)-0.5
data$polarizftabs <- abs(data$ft_dem-data$ft_rep)
data$polarizcsesabs <- abs(data$cses_dem-data$cses_rep)
## Year
data$year <- data$VCF0004
### Z-scored measures
data <- data %>% group_by(year) %>% dplyr::mutate(inpartyft_z=scale(inpartyft)[,1],
                                                  outpartyft_z=scale(outpartyft)[,1],
                                                  polarizft_z=scale(polarizft)[,1],
                                                  inpartycses_z=scale(inpartycses)[,1],
                                                  outpartycses_z=scale(outpartycses)[,1],
                                                  polarizcses_z=scale(polarizcses)[,1])
## State
data$state <- data$VCF0901a
anes20$state <- anes20$V203000

## Congressional District
anes20$district <- ifelse(anes20$V203002==1,"01",ifelse(anes20$V203002==2,"02",ifelse(anes20$V203002==3,"03",ifelse(anes20$V203002==4,"04",ifelse(anes20$V203002==5,"05",ifelse(anes20$V203002==6,"06",ifelse(anes20$V203002==7,"07",ifelse(anes20$V203002==8,"08",ifelse(anes20$V203002==9,"09",as.character(anes20$V203002))))))))))

## State-District
data$statedistrict <- data$VCF0900b
anes20$statedistrict <- as.numeric(as.character(paste(anes20$state,anes20$district,sep="",collapse=NULL)))
data$V200001 <- ifelse(data$year==2020,data$VCF0006,NA)
data$statedistrict[match(data$V200001,anes20$V200001) & is.na(data$V200001)==F] <- anes20$statedistrict

## Other Vars
#data$trend <- data$VCF0004-1978
data$pid_strength <- abs(data$pid-4)
data$interest <- ifelse(data$VCF0313==0 | data$VCF0313==9,NA,data$VCF0313)
data$ideo_extremity <- ifelse(data$VCF0803==0 | data$VCF0803==9,NA,abs(data$VCF0803-4))
data$age <- ifelse(data$VCF0101==0,NA,data$VCF0101)
data$female <- ifelse(data$VCF0104==0,NA,ifelse(data$VCF0104==1 | data$VCF0104==3,1,0))
data$white <- ifelse(data$VCF0105b==1,1,ifelse(data$VCF0105b==0 | data$VCF0105b==9,NA,0))
data$education <- ifelse(data$VCF0140a>8,NA,data$VCF0140a)
data$urbanity <- ifelse(data$VCF0111==0,NA,3-data$VCF0111)
data$south <- 2-data$VCF0113
data$income <- ifelse(data$VCF0114==0,NA,ifelse(data$VCF0114==1,1,ifelse(data$VCF0114==2,17,ifelse(data$VCF0114==3,34,ifelse(data$VCF0114==4,68,96)))))
data$church <- ifelse(data$VCF0130>7 | data$VCF0130==0,NA,ifelse(data$VCF0130==5 | data$VCF0130==7,0,5-data$VCF0130))

# Order House Data (getting rid of third parties and write-ins, switching to long, taking Democratic two-party vote)
house <- subset(house,(party=="DEMOCRAT" | party=="DEMOCRATIC" | party=="REPUBLICAN" | party=="DEMOCRATIC-FARM-LABOR" | party=="DEMOCRATIC-FARMER-LABOR" | party=="DEMOCRATIC-NONPARTISAN LEAGUE") & stage=="GEN" & special==F & (runoff==F | is.na(runoff)==T))
house$party[house$party=="DEMOCRATIC-FARM-LABOR" | house$party=="DEMOCRATIC-FARMER-LABOR" | house$party=="DEMOCRATIC-NONPARTISAN LEAGUE" | house$party=="DEMOCRATIC"] <- "DEMOCRAT"
house1 <- house %>% spread(party,candidatevotes)
house1$democrat <- as.numeric(as.character(house1$DEMOCRAT))
house1$republican <- as.numeric(as.character(house1$REPUBLICAN))
house2 <- house1 %>% group_by(year,state_fips,district) %>% summarise(dem=mean(democrat,na.rm=T),rep=mean(republican,na.rm=T))
house2$dem[is.na(house2$dem==T)] <- 0
house2$rep[is.na(house2$rep==T)] <- 0
house2$total <- house2$dem + house2$rep
house2$dem.twoparty <- ((house2$dem/house2$total) * 100) - 50
house2$district.new <- ifelse(house2$district==1,"01",ifelse(house2$district==2,"02",ifelse(house2$district==3,"03",ifelse(house2$district==4,"04",ifelse(house2$district==5,"05",ifelse(house2$district==6,"06",ifelse(house2$district==7,"07",ifelse(house2$district==8,"08",ifelse(house2$district==9,"09",ifelse(house2$district==0,"01",as.character(house2$district)))))))))))
house2$VCF0900b <- paste(house2$state_fips,house2$district.new,sep="",collapse=NULL)
house2$VCF0900b <- as.numeric(as.character(house2$VCF0900b))
house2$statedistrict <- house2$VCF0900b
house2$VCF0004 <- house2$year
house2$dem.twoparty.lag <- lag(house2$dem.twoparty,order_by=house2$statedistrict)
house2$dem.twoparty.lag <- ifelse(house2$year==1976,NA,house2$dem.twoparty.lag)
house3 <- subset(house2,year==1996 | year==2004 | year==2008 | year==2012 | year==2016 | year==2020)

# Order Senate Data (Louisiana 2002 presented problems)
senate <- subset(senate,(party_simplified=="DEMOCRAT" | party_simplified=="REPUBLICAN") & stage=="gen" & special=="False")
senate <- senate[-c(875:881), ] ## Get rid of Louisiana 2002
senate1 <- senate %>% spread(party_detailed,candidatevotes)
senate1$democrat <- as.numeric(as.character(senate1$DEMOCRAT))
senate1$republican <- as.numeric(as.character(senate1$REPUBLICAN))
senate2 <- senate1 %>% group_by(year,state_fips) %>% summarise(dem=mean(democrat,na.rm=T),rep=mean(republican,na.rm=T))
senate2$dem[is.na(senate2$dem==T)] <- 0
senate2$rep[is.na(senate2$rep==T)] <- 0
senate2$total <- senate2$dem + senate2$rep
senate2$dem.twoparty <- ((senate2$dem/senate2$total) * 100) - 50
senate2$state <- senate2$state_fips
senate2$VCF0004 <- senate2$year
senate2$dem.twoparty.lag <- lag(senate2$dem.twoparty,order_by=senate2$state)
senate2$dem.twoparty.lag[senate2$year==1976 & (senate2$state==4 | senate2$state==6 | senate2$state==9 | senate2$state==10 | senate2$state==12 | senate2$state==15 | senate2$state==18 | senate2$state==23 | senate2$state==24 | senate2$state==25 | senate2$state==26 | senate2$state==27 | senate2$state==28 | senate2$state==29 | senate2$state==30 | senate2$state==31 | senate2$state==32 | senate2$state==34 | senate2$state==35 | senate2$state==36 | senate2$state==38 | senate2$state==39 | senate2$state==42 | senate2$state==44 | senate2$state==47 | senate2$state==48 | senate2$state==49 | senate2$state==50 | senate2$state==51 | senate2$state==53 | senate2$state==54 | senate2$state==55 | senate2$state==56)] <- NA
senate2$dem.twoparty.lag[senate2$year==1978 & (senate2$state==1 | senate2$state==2 | senate2$state==5 | senate2$state==8 | senate2$state==13 | senate2$state==16 | senate2$state==17 | senate2$state==19 | senate2$state==20 | senate2$state==21 | senate2$state==22 | senate2$state==33 | senate2$state==37 | senate2$state==40 | senate2$state==41 | senate2$state==45 | senate2$state==46)] <- NA
senate3 <- subset(senate2,year==1996 | year==2004 | year==2008 | year==2012 | year==2016 | year==2020)

# Order Presidential Data
president <- subset(president,(party_simplified=="DEMOCRAT" | party_simplified=="REPUBLICAN"))
president1 <- president %>% spread(party_detailed,candidatevotes)
president1$democrat <- as.numeric(as.character(president1$DEMOCRAT))
president1$republican <- as.numeric(as.character(president1$REPUBLICAN))
president2 <- president1 %>% group_by(year, state_fips) %>% summarise(dem=mean(democrat,na.rm=T),rep=mean(republican,na.rm=T))
president2$dem[is.na(president2$dem==T)] <- 0
president2$rep[is.na(president2$rep==T)] <- 0
president2$total <- president2$dem + president2$rep
president2$dem.twoparty <- ((president2$dem/president2$total) * 100) - 50
president2$state <- president2$state_fips
president2$dem.twoparty.lag <- lag(president2$dem.twoparty,order_by=president2$state)
president2$dem.twoparty.lag[president2$year==1976] <- NA
president3 <- subset(president2,year==1996 | year==2004 | year==2008 | year==2012 | year==2016 | year==2020)

# Merge Datasets
## House
data.house <- merge(data,house2,by=c("statedistrict","year"))
data.house$winner <- ifelse(data.house$pid==4,NA,ifelse(data.house$pid<4 & data.house$dem.twoparty>0 | data.house$pid>4 & data.house$dem.twoparty<0,1,0))
data.house$winner.lag <- ifelse(data.house$pid==4,NA,ifelse(data.house$pid<4 & data.house$dem.twoparty.lag>0 | data.house$pid>4 & data.house$dem.twoparty.lag<0,1,0))
data.house$winner.twoparty <- ifelse(data.house$pid<4,data.house$dem.twoparty,ifelse(data.house$pid>4,data.house$dem.twoparty*(-1),NA))
data.house$winner.twoparty.lag <- ifelse(data.house$pid<4,data.house$dem.twoparty.lag,ifelse(data.house$pid>4,data.house$dem.twoparty.lag*(-1),NA))
data.house$margin <- abs(data.house$winner.twoparty)/50
## Senate
data.senate <- merge(data,senate2,by=c("state","year"))
data.senate$winner <- ifelse(data.senate$pid==4,NA,ifelse(data.senate$pid<4 & data.senate$dem.twoparty>0 | data.senate$pid>4 & data.senate$dem.twoparty<0,1,0))
data.senate$winner.lag <- ifelse(data.senate$pid==4,NA,ifelse(data.senate$pid<4 & data.senate$dem.twoparty.lag>0 | data.senate$pid>4 & data.senate$dem.twoparty.lag<0,1,0))
data.senate$winner.twoparty <- ifelse(data.senate$pid<4,data.senate$dem.twoparty,ifelse(data.senate$pid>4,data.senate$dem.twoparty*(-1),NA))
data.senate$winner.twoparty.lag <- ifelse(data.senate$pid<4,data.senate$dem.twoparty.lag,ifelse(data.senate$pid>4,data.senate$dem.twoparty.lag*(-1),NA))
data.senate$margin <- abs(data.senate$winner.twoparty)/50
## President
data.president <- merge(data,president2,by=c("state","year"))
data.president$winner <- ifelse(data.president$pid==4,NA,ifelse(data.president$pid<4 & data.president$dem.twoparty>0 | data.president$pid>4 & data.president$dem.twoparty<0,1,0))
data.president$winner.lag <- ifelse(data.president$pid==4,NA,ifelse(data.president$pid<4 & data.president$dem.twoparty.lag>0 | data.president$pid>4 & data.president$dem.twoparty.lag<0,1,0))
data.president$winner.twoparty <- ifelse(data.president$pid<4,data.president$dem.twoparty,ifelse(data.president$pid>4,data.president$dem.twoparty*(-1),NA))
data.president$winner.twoparty.lag <- ifelse(data.president$pid<4,data.president$dem.twoparty.lag,ifelse(data.president$pid>4,data.president$dem.twoparty.lag*(-1),NA))
data.president$margin <- abs(data.president$winner.twoparty)/50

# Create Datasets for RDDs
data.house$fyear <- factor(data.house$year)
data.senate$fyear <- factor(data.senate$year)
data.president$fyear <- factor(data.president$year)
data.house.l <- subset(data.house,year==1996 | year==2004 | year==2008 | year==2012 | year==2016 | year==2020)
data.senate.l <- subset(data.senate,year==1996 | year==2004 | year==2008 | year==2012 | year==2016 | year==2020)
data.president.l <- subset(data.president,year==1996 | year==2004 | year==2008 | year==2012 | year==2016 | year==2020)

# Table 2, Number of Races and ANES Observations by RDD bandwidth
data.house.l$electionyear <- paste(as.character(data.house.l$statedistrict),"",as.character(data.house.l$fyear))
data.senate.l$electionyear <- paste(as.character(data.senate.l$state),"",as.character(data.senate.l$fyear))
data.president.l$electionyear <- paste(as.character(data.president.l$state),"",as.character(data.president.l$fyear))
## House Elections
### Number of Elections
length(unique(subset(data.house.l,abs(winner.twoparty)<=1)$electionyear)) # 1% Margin
length(unique(subset(data.house.l,abs(winner.twoparty)<=2)$electionyear)) # 2% Margin
length(unique(subset(data.house.l,abs(winner.twoparty)<=3)$electionyear)) # 3% Margin
length(unique(subset(data.house.l,abs(winner.twoparty)<=4)$electionyear)) # 4% Margin
length(unique(subset(data.house.l,abs(winner.twoparty)<=5)$electionyear)) # 5% Margin
length(unique(subset(data.house.l,abs(winner.twoparty)<=11.04)$electionyear)) # Optimal Threshold
length(unique(data.house.l$electionyear)) # All
### Number of Respondents
summary(abs(data.house.l$winner.twoparty)<=1) # 1% Margin
summary(abs(data.house.l$winner.twoparty)<=2) # 2% Margin
summary(abs(data.house.l$winner.twoparty)<=3) # 3% Margin
summary(abs(data.house.l$winner.twoparty)<=4) # 4% Margin
summary(abs(data.house.l$winner.twoparty)<=5) # 5% Margin
summary(abs(data.house.l$winner.twoparty)<=11.04) # 5% Margin
length(na.omit(data.house.l$winner.twoparty)) # All
## Senate Elections
### Number of Elections
length(unique(subset(data.senate.l,abs(winner.twoparty)<=1)$electionyear)) # 1% Margin
length(unique(subset(data.senate.l,abs(winner.twoparty)<=2)$electionyear)) # 2% Margin
length(unique(subset(data.senate.l,abs(winner.twoparty)<=3)$electionyear)) # 3% Margin
length(unique(subset(data.senate.l,abs(winner.twoparty)<=4)$electionyear)) # 4% Margin
length(unique(subset(data.senate.l,abs(winner.twoparty)<=5)$electionyear)) # 5% Margin
length(unique(subset(data.senate.l,abs(winner.twoparty)<=9.85)$electionyear)) # Optimal Threshold
length(unique(data.senate.l$electionyear)) # All
### Number of Respondents
summary(abs(data.senate.l$winner.twoparty)<=1) # 1% Margin
summary(abs(data.senate.l$winner.twoparty)<=2) # 2% Margin
summary(abs(data.senate.l$winner.twoparty)<=3) # 3% Margin
summary(abs(data.senate.l$winner.twoparty)<=4) # 4% Margin
summary(abs(data.senate.l$winner.twoparty)<=5) # 5% Margin
summary(abs(data.senate.l$winner.twoparty)<=9.85) # Optimal Threshold
length(na.omit(data.senate.l$winner.twoparty)) # All
## Presidential Elections
### Number of Elections
length(unique(subset(data.president.l,abs(winner.twoparty)<=1)$electionyear)) # 1% Margin
length(unique(subset(data.president.l,abs(winner.twoparty)<=2)$electionyear)) # 2% Margin
length(unique(subset(data.president.l,abs(winner.twoparty)<=3)$electionyear)) # 3% Margin
length(unique(subset(data.president.l,abs(winner.twoparty)<=4)$electionyear)) # 4% Margin
length(unique(subset(data.president.l,abs(winner.twoparty)<=5)$electionyear)) # 5% Margin
length(unique(subset(data.president.l,abs(winner.twoparty)<=8.67)$electionyear)) # Optimal Threshold
length(unique(data.president.l$electionyear)) # All
### Number of Respondents
summary(abs(data.president.l$winner.twoparty)<=1) # 1% Margin
summary(abs(data.president.l$winner.twoparty)<=2) # 2% Margin
summary(abs(data.president.l$winner.twoparty)<=3) # 3% Margin
summary(abs(data.president.l$winner.twoparty)<=4) # 4% Margin
summary(abs(data.president.l$winner.twoparty)<=5) # 5% Margin
summary(abs(data.president.l$winner.twoparty)<=8.67) # Optimal Threshold
length(na.omit(data.president.l$winner.twoparty)) # All

# Regression Discontinuities and Associated Figures
## House RDDs
### Affective Polarization (Table A6)
hpl.change1 <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$polarizcses,covs=data.house.l$polarizft+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2",h=1)
hpl.change2 <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$polarizcses,covs=data.house.l$polarizft+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2",h=2)
hpl.change3 <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$polarizcses,covs=data.house.l$polarizft+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2",h=3)
hpl.change4 <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$polarizcses,covs=data.house.l$polarizft+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2",h=4)
hpl.change5 <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$polarizcses,covs=data.house.l$polarizft+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2",h=5)
hpl.change.opt <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$polarizcses,covs=data.house.l$polarizft+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2")
### In-Party Warmth (Table A7)
hil.change1 <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$inpartycses,covs=data.house.l$inpartyft+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2",h=1)
hil.change2 <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$inpartycses,covs=data.house.l$inpartyft+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2",h=2)
hil.change3 <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$inpartycses,covs=data.house.l$inpartyft+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2",h=3)
hil.change4 <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$inpartycses,covs=data.house.l$inpartyft+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2",h=4)
hil.change5 <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$inpartycses,covs=data.house.l$inpartyft+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2",h=5)
hil.change.opt <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$inpartycses,covs=data.house.l$inpartyft+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2")
### Out-Party Warmth (Table A8)
hol.change1 <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$outpartycses,covs=data.house.l$outpartyft+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2",h=1)
hol.change2 <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$outpartycses,covs=data.house.l$outpartyft+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2",h=2)
hol.change3 <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$outpartycses,covs=data.house.l$outpartyft+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2",h=3)
hol.change4 <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$outpartycses,covs=data.house.l$outpartyft+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2",h=4)
hol.change5 <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$outpartycses,covs=data.house.l$outpartyft+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2",h=5)
hol.change.opt <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$outpartycses,covs=data.house.l$outpartyft+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2")
## Senate RDDs
### Affective Polarization (Table A9)
spl.change1 <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$polarizcses,covs=data.senate.l$polarizft+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2",h=1)
spl.change2 <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$polarizcses,covs=data.senate.l$polarizft+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2",h=2)
spl.change3 <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$polarizcses,covs=data.senate.l$polarizft+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2",h=3)
spl.change4 <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$polarizcses,covs=data.senate.l$polarizft+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2",h=4)
spl.change5 <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$polarizcses,covs=data.senate.l$polarizft+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2",h=5)
spl.change.opt <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$polarizcses,covs=data.senate.l$polarizft+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2")
### In-Party Warmth (Table A10)
sil.change1 <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$inpartycses,covs=data.senate.l$inpartyft+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2",h=1)
sil.change2 <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$inpartycses,covs=data.senate.l$inpartyft+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2",h=2)
sil.change3 <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$inpartycses,covs=data.senate.l$inpartyft+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2",h=3)
sil.change4 <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$inpartycses,covs=data.senate.l$inpartyft+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2",h=4)
sil.change5 <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$inpartycses,covs=data.senate.l$inpartyft+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2",h=5)
sil.change.opt <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$inpartycses,covs=data.senate.l$inpartyft+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2")
### Out-Party Warmth (Table A11)
sol.change1 <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$outpartycses,covs=data.senate.l$outpartyft+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2",h=1)
sol.change2 <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$outpartycses,covs=data.senate.l$outpartyft+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2",h=2)
sol.change3 <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$outpartycses,covs=data.senate.l$outpartyft+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2",h=3)
sol.change4 <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$outpartycses,covs=data.senate.l$outpartyft+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2",h=4)
sol.change5 <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$outpartycses,covs=data.senate.l$outpartyft+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2",h=5)
sol.change.opt <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$outpartycses,covs=data.senate.l$outpartyft+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2")
## Presidential RDDs
### Affective Polarization (Table A12)
ppl.change1 <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$polarizcses,covs=data.president.l$polarizft+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2",h=1)
ppl.change2 <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$polarizcses,covs=data.president.l$polarizft+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2",h=2)
ppl.change3 <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$polarizcses,covs=data.president.l$polarizft+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2",h=3)
ppl.change4 <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$polarizcses,covs=data.president.l$polarizft+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2",h=4)
ppl.change5 <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$polarizcses,covs=data.president.l$polarizft+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2",h=5)
ppl.change.opt <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$polarizcses,covs=data.president.l$polarizft+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2")
### In-Party Warmth (Table A13)
pil.change1 <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$inpartycses,covs=data.president.l$inpartyft+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2",h=1)
pil.change2 <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$inpartycses,covs=data.president.l$inpartyft+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2",h=2)
pil.change3 <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$inpartycses,covs=data.president.l$inpartyft+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2",h=3)
pil.change4 <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$inpartycses,covs=data.president.l$inpartyft+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2",h=4)
pil.change5 <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$inpartycses,covs=data.president.l$inpartyft+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2",h=5)
pil.change.opt <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$inpartycses,covs=data.president.l$inpartyft+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2")
### Out-Party Warmth (Table A14)
pol.change1 <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$outpartycses,covs=data.president.l$outpartyft+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2",h=1)
pol.change2 <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$outpartycses,covs=data.president.l$outpartyft+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2",h=2)
pol.change3 <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$outpartycses,covs=data.president.l$outpartyft+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2",h=3)
pol.change4 <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$outpartycses,covs=data.president.l$outpartyft+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2",h=4)
pol.change5 <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$outpartycses,covs=data.president.l$outpartyft+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2",h=5)
pol.change.opt <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$outpartycses,covs=data.president.l$outpartyft+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2")
## Figure A5 (Robust Bias-Corrected, All Thresholds)
plotdata.rdd.robust <- data.frame(election=factor(c(rep("House, Pol",6),rep("House, In",6),rep("House, Out",6),rep("Senate, Pol",6),rep("Senate, In",6),rep("Senate, Out",6),rep("President, Pol",6),rep("President, In",6),rep("President, Out",6)),levels=c("House, Pol","House, In","House, Out","Senate, Pol","Senate, In","Senate, Out","President, Pol","President, In","President, Out")),
                                  margin=factor(c(rep(c("1%","2%","3%","4%","5%","Optimal"),9)),levels=c("Optimal","5%","4%","3%","2%","1%")),
                                  coef=c(hpl.change1$coef[3],hpl.change2$coef[3],hpl.change3$coef[3],hpl.change4$coef[3],hpl.change5$coef[3],hpl.change.opt$coef[3], hil.change1$coef[3],hil.change2$coef[3],hil.change3$coef[3],hil.change4$coef[3],hil.change5$coef[3],hil.change.opt$coef[3], hol.change1$coef[3],hol.change2$coef[3],hol.change3$coef[3],hol.change4$coef[3],hol.change5$coef[3],hol.change.opt$coef[3], spl.change1$coef[3],spl.change2$coef[3],spl.change3$coef[3],spl.change4$coef[3],spl.change5$coef[3],spl.change.opt$coef[3], sil.change1$coef[3],sil.change2$coef[3],sil.change3$coef[3],sil.change4$coef[3],sil.change5$coef[3],sil.change.opt$coef[3], sol.change1$coef[3],sol.change2$coef[3],sol.change3$coef[3],sol.change4$coef[3],sol.change5$coef[3],sol.change.opt$coef[3], ppl.change1$coef[3],ppl.change2$coef[3],ppl.change3$coef[3],ppl.change4$coef[3],ppl.change5$coef[3],ppl.change.opt$coef[3], pil.change1$coef[3],pil.change2$coef[3],pil.change3$coef[3],pil.change4$coef[3],pil.change5$coef[3],pil.change.opt$coef[3], pol.change1$coef[3],pol.change2$coef[3],pol.change3$coef[3],pol.change4$coef[3],pol.change5$coef[3],pol.change.opt$coef[3]),
                                  lci=c(hpl.change1$ci[3],hpl.change2$ci[3],hpl.change3$ci[3],hpl.change4$ci[3],hpl.change5$ci[3],hpl.change.opt$ci[3], hil.change1$ci[3],hil.change2$ci[3],hil.change3$ci[3],hil.change4$ci[3],hil.change5$ci[3],hil.change.opt$ci[3], hol.change1$ci[3],hol.change2$ci[3],hol.change3$ci[3],hol.change4$ci[3],hol.change5$ci[3],hol.change.opt$ci[3], spl.change1$ci[3],spl.change2$ci[3],spl.change3$ci[3],spl.change4$ci[3],spl.change5$ci[3],spl.change.opt$ci[3], sil.change1$ci[3],sil.change2$ci[3],sil.change3$ci[3],sil.change4$ci[3],sil.change5$ci[3],sil.change.opt$ci[3], sol.change1$ci[3],sol.change2$ci[3],sol.change3$ci[3],sol.change4$ci[3],sol.change5$ci[3],sol.change.opt$ci[3], ppl.change1$ci[3],ppl.change2$ci[3],ppl.change3$ci[3],ppl.change4$ci[3],ppl.change5$ci[3],ppl.change.opt$ci[3], pil.change1$ci[3],pil.change2$ci[3],pil.change3$ci[3],pil.change4$ci[3],pil.change5$ci[3],pil.change.opt$ci[3], pol.change1$ci[3],pol.change2$ci[3],pol.change3$ci[3],pol.change4$ci[3],pol.change5$ci[3],pol.change.opt$ci[3]),
                                  uci=c(hpl.change1$ci[6],hpl.change2$ci[6],hpl.change3$ci[6],hpl.change4$ci[6],hpl.change5$ci[6],hpl.change.opt$ci[6], hil.change1$ci[6],hil.change2$ci[6],hil.change3$ci[6],hil.change4$ci[6],hil.change5$ci[6],hil.change.opt$ci[6], hol.change1$ci[6],hol.change2$ci[6],hol.change3$ci[6],hol.change4$ci[6],hol.change5$ci[6],hol.change.opt$ci[6], spl.change1$ci[6],spl.change2$ci[6],spl.change3$ci[6],spl.change4$ci[6],spl.change5$ci[6],spl.change.opt$ci[6], sil.change1$ci[6],sil.change2$ci[6],sil.change3$ci[6],sil.change4$ci[6],sil.change5$ci[6],sil.change.opt$ci[6], sol.change1$ci[6],sol.change2$ci[6],sol.change3$ci[6],sol.change4$ci[6],sol.change5$ci[6],sol.change.opt$ci[6], ppl.change1$ci[6],ppl.change2$ci[6],ppl.change3$ci[6],ppl.change4$ci[6],ppl.change5$ci[6],ppl.change.opt$ci[6], pil.change1$ci[6],pil.change2$ci[6],pil.change3$ci[6],pil.change4$ci[6],pil.change5$ci[6],pil.change.opt$ci[6], pol.change1$ci[6],pol.change2$ci[6],pol.change3$ci[6],pol.change4$ci[6],pol.change5$ci[6],pol.change.opt$ci[6]))


p.rdd.robust <- ggplot(plotdata.rdd.robust,aes(x=margin,y=coef)) + geom_point() + geom_errorbar(aes(ymin=lci,ymax=uci),lwd=0.5,width=0) + theme_classic() + coord_flip() + xlab("Margin") + ylab("Effect") + geom_hline(yintercept=0,linetype="dashed",color="red") + facet_wrap(vars(election),nrow=3,ncol=3)
## Figure A3 (Bias-Corrected)
plotdata.rdd.biascorrected <- data.frame(election=factor(c(rep("House, Pol",6),rep("House, In",6),rep("House, Out",6),rep("Senate, Pol",6),rep("Senate, In",6),rep("Senate, Out",6),rep("President, Pol",6),rep("President, In",6),rep("President, Out",6)),levels=c("House, Pol","House, In","House, Out","Senate, Pol","Senate, In","Senate, Out","President, Pol","President, In","President, Out")),
                                         margin=factor(c(rep(c("1%","2%","3%","4%","5%","Optimal"),9)),levels=c("Optimal","5%","4%","3%","2%","1%")),
                                         coef=c(hpl.change1$coef[2],hpl.change2$coef[2],hpl.change3$coef[2],hpl.change4$coef[2],hpl.change5$coef[2],hpl.change.opt$coef[2], hil.change1$coef[2],hil.change2$coef[2],hil.change3$coef[2],hil.change4$coef[2],hil.change5$coef[2],hil.change.opt$coef[2], hol.change1$coef[2],hol.change2$coef[2],hol.change3$coef[2],hol.change4$coef[2],hol.change5$coef[2],hol.change.opt$coef[2], spl.change1$coef[2],spl.change2$coef[2],spl.change3$coef[2],spl.change4$coef[2],spl.change5$coef[2],spl.change.opt$coef[2], sil.change1$coef[2],sil.change2$coef[2],sil.change3$coef[2],sil.change4$coef[2],sil.change5$coef[2],sil.change.opt$coef[2], sol.change1$coef[2],sol.change2$coef[2],sol.change3$coef[2],sol.change4$coef[2],sol.change5$coef[2],sol.change.opt$coef[2], ppl.change1$coef[2],ppl.change2$coef[2],ppl.change3$coef[2],ppl.change4$coef[2],ppl.change5$coef[2],ppl.change.opt$coef[2], pil.change1$coef[2],pil.change2$coef[2],pil.change3$coef[2],pil.change4$coef[2],pil.change5$coef[2],pil.change.opt$coef[2], pol.change1$coef[2],pol.change2$coef[2],pol.change3$coef[2],pol.change4$coef[2],pol.change5$coef[2],pol.change.opt$coef[2]),
                                         lci=c(hpl.change1$ci[2],hpl.change2$ci[2],hpl.change3$ci[2],hpl.change4$ci[2],hpl.change5$ci[2],hpl.change.opt$ci[2], hil.change1$ci[2],hil.change2$ci[2],hil.change3$ci[2],hil.change4$ci[2],hil.change5$ci[2],hil.change.opt$ci[2], hol.change1$ci[2],hol.change2$ci[2],hol.change3$ci[2],hol.change4$ci[2],hol.change5$ci[2],hol.change.opt$ci[2], spl.change1$ci[2],spl.change2$ci[2],spl.change3$ci[2],spl.change4$ci[2],spl.change5$ci[2],spl.change.opt$ci[2], sil.change1$ci[2],sil.change2$ci[2],sil.change3$ci[2],sil.change4$ci[2],sil.change5$ci[2],sil.change.opt$ci[2], sol.change1$ci[2],sol.change2$ci[2],sol.change3$ci[2],sol.change4$ci[2],sol.change5$ci[2],sol.change.opt$ci[2], ppl.change1$ci[2],ppl.change2$ci[2],ppl.change3$ci[2],ppl.change4$ci[2],ppl.change5$ci[2],ppl.change.opt$ci[2], pil.change1$ci[2],pil.change2$ci[2],pil.change3$ci[2],pil.change4$ci[2],pil.change5$ci[2],pil.change.opt$ci[2], pol.change1$ci[2],pol.change2$ci[2],pol.change3$ci[2],pol.change4$ci[2],pol.change5$ci[2],pol.change.opt$ci[2]),
                                         uci=c(hpl.change1$ci[5],hpl.change2$ci[5],hpl.change3$ci[5],hpl.change4$ci[5],hpl.change5$ci[5],hpl.change.opt$ci[5], hil.change1$ci[5],hil.change2$ci[5],hil.change3$ci[5],hil.change4$ci[5],hil.change5$ci[5],hil.change.opt$ci[5], hol.change1$ci[5],hol.change2$ci[5],hol.change3$ci[5],hol.change4$ci[5],hol.change5$ci[5],hol.change.opt$ci[5], spl.change1$ci[5],spl.change2$ci[5],spl.change3$ci[5],spl.change4$ci[5],spl.change5$ci[5],spl.change.opt$ci[5], sil.change1$ci[5],sil.change2$ci[5],sil.change3$ci[5],sil.change4$ci[5],sil.change5$ci[5],sil.change.opt$ci[5], sol.change1$ci[5],sol.change2$ci[5],sol.change3$ci[5],sol.change4$ci[5],sol.change5$ci[5],sol.change.opt$ci[5], ppl.change1$ci[5],ppl.change2$ci[5],ppl.change3$ci[5],ppl.change4$ci[5],ppl.change5$ci[5],ppl.change.opt$ci[5], pil.change1$ci[5],pil.change2$ci[5],pil.change3$ci[5],pil.change4$ci[5],pil.change5$ci[5],pil.change.opt$ci[5], pol.change1$ci[5],pol.change2$ci[5],pol.change3$ci[5],pol.change4$ci[5],pol.change5$ci[5],pol.change.opt$ci[5]))

p.rdd.biascorrected <- ggplot(plotdata.rdd.biascorrected,aes(x=margin,y=coef)) + geom_point() + geom_errorbar(aes(ymin=lci,ymax=uci),lwd=0.5,width=0) + theme_classic() + coord_flip() + xlab("Margin") + ylab("Effect") + geom_hline(yintercept=0,linetype="dashed",color="red") + facet_wrap(vars(election),nrow=3,ncol=3)
## Figure A4 (Conventional)
plotdata.rdd.conventional <- data.frame(election=factor(c(rep("House, Pol",6),rep("House, In",6),rep("House, Out",6),rep("Senate, Pol",6),rep("Senate, In",6),rep("Senate, Out",6),rep("President, Pol",6),rep("President, In",6),rep("President, Out",6)),levels=c("House, Pol","House, In","House, Out","Senate, Pol","Senate, In","Senate, Out","President, Pol","President, In","President, Out")),
                                        margin=factor(c(rep(c("1%","2%","3%","4%","5%","Optimal"),9)),levels=c("Optimal","5%","4%","3%","2%","1%")),
                                        coef=c(hpl.change1$coef[1],hpl.change2$coef[1],hpl.change3$coef[1],hpl.change4$coef[1],hpl.change5$coef[1],hpl.change.opt$coef[1], hil.change1$coef[1],hil.change2$coef[1],hil.change3$coef[1],hil.change4$coef[1],hil.change5$coef[1],hil.change.opt$coef[1], hol.change1$coef[1],hol.change2$coef[1],hol.change3$coef[1],hol.change4$coef[1],hol.change5$coef[1],hol.change.opt$coef[1], spl.change1$coef[1],spl.change2$coef[1],spl.change3$coef[1],spl.change4$coef[1],spl.change5$coef[1],spl.change.opt$coef[1], sil.change1$coef[1],sil.change2$coef[1],sil.change3$coef[1],sil.change4$coef[1],sil.change5$coef[1],sil.change.opt$coef[1], sol.change1$coef[1],sol.change2$coef[1],sol.change3$coef[1],sol.change4$coef[1],sol.change5$coef[1],sol.change.opt$coef[1], ppl.change1$coef[1],ppl.change2$coef[1],ppl.change3$coef[1],ppl.change4$coef[1],ppl.change5$coef[1],ppl.change.opt$coef[1], pil.change1$coef[1],pil.change2$coef[1],pil.change3$coef[1],pil.change4$coef[1],pil.change5$coef[1],pil.change.opt$coef[1], pol.change1$coef[1],pol.change2$coef[1],pol.change3$coef[1],pol.change4$coef[1],pol.change5$coef[1],pol.change.opt$coef[1]),
                                        lci=c(hpl.change1$ci[1],hpl.change2$ci[1],hpl.change3$ci[1],hpl.change4$ci[1],hpl.change5$ci[1],hpl.change.opt$ci[1], hil.change1$ci[1],hil.change2$ci[1],hil.change3$ci[1],hil.change4$ci[1],hil.change5$ci[1],hil.change.opt$ci[1], hol.change1$ci[1],hol.change2$ci[1],hol.change3$ci[1],hol.change4$ci[1],hol.change5$ci[1],hol.change.opt$ci[1], spl.change1$ci[1],spl.change2$ci[1],spl.change3$ci[1],spl.change4$ci[1],spl.change5$ci[1],spl.change.opt$ci[1], sil.change1$ci[1],sil.change2$ci[1],sil.change3$ci[1],sil.change4$ci[1],sil.change5$ci[1],sil.change.opt$ci[1], sol.change1$ci[1],sol.change2$ci[1],sol.change3$ci[1],sol.change4$ci[1],sol.change5$ci[1],sol.change.opt$ci[1], ppl.change1$ci[1],ppl.change2$ci[1],ppl.change3$ci[1],ppl.change4$ci[1],ppl.change5$ci[1],ppl.change.opt$ci[1], pil.change1$ci[1],pil.change2$ci[1],pil.change3$ci[1],pil.change4$ci[1],pil.change5$ci[1],pil.change.opt$ci[1], pol.change1$ci[1],pol.change2$ci[1],pol.change3$ci[1],pol.change4$ci[1],pol.change5$ci[1],pol.change.opt$ci[1]),
                                        uci=c(hpl.change1$ci[4],hpl.change2$ci[4],hpl.change3$ci[4],hpl.change4$ci[4],hpl.change5$ci[4],hpl.change.opt$ci[4], hil.change1$ci[4],hil.change2$ci[4],hil.change3$ci[4],hil.change4$ci[4],hil.change5$ci[4],hil.change.opt$ci[4], hol.change1$ci[4],hol.change2$ci[4],hol.change3$ci[4],hol.change4$ci[4],hol.change5$ci[4],hol.change.opt$ci[4], spl.change1$ci[4],spl.change2$ci[4],spl.change3$ci[4],spl.change4$ci[4],spl.change5$ci[4],spl.change.opt$ci[4], sil.change1$ci[4],sil.change2$ci[4],sil.change3$ci[4],sil.change4$ci[4],sil.change5$ci[4],sil.change.opt$ci[4], sol.change1$ci[4],sol.change2$ci[4],sol.change3$ci[4],sol.change4$ci[4],sol.change5$ci[4],sol.change.opt$ci[4], ppl.change1$ci[4],ppl.change2$ci[4],ppl.change3$ci[4],ppl.change4$ci[4],ppl.change5$ci[4],ppl.change.opt$ci[4], pil.change1$ci[4],pil.change2$ci[4],pil.change3$ci[4],pil.change4$ci[4],pil.change5$ci[4],pil.change.opt$ci[4], pol.change1$ci[4],pol.change2$ci[4],pol.change3$ci[4],pol.change4$ci[4],pol.change5$ci[4],pol.change.opt$ci[4]))

p.rdd.conventional <- ggplot(plotdata.rdd.conventional,aes(x=margin,y=coef)) + geom_point() + geom_errorbar(aes(ymin=lci,ymax=uci),lwd=0.5,width=0) + theme_classic() + coord_flip() + xlab("Margin") + ylab("Effect") + geom_hline(yintercept=0,linetype="dashed",color="red") + facet_wrap(vars(election),nrow=3,ncol=3)
## Figure 2 (Simplified, Few Thresholds)
plotdata.rdd.robust.simple <- subset(plotdata.rdd.robust,margin=="3%" | margin=="Optimal")
p.rdd.robust.simple <- ggplot(plotdata.rdd.robust.simple,aes(x=margin,y=coef)) + geom_point() + geom_errorbar(aes(ymin=lci,ymax=uci),lwd=0.5,width=0) + theme_classic() + coord_flip() + xlab("Margin") + ylab("Effect") + geom_hline(yintercept=0,linetype="dashed",color="red") + facet_wrap(vars(election),nrow=3,ncol=3)

# Table A3, Balance Tests (House)
pidstrength.h.nolag.1 <- lm(pid_strength~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=1 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
pidstrength.h.nolag.2 <- lm(pid_strength~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=2 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
pidstrength.h.nolag.3 <- lm(pid_strength~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=3 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
pidstrength.h.nolag.4 <- lm(pid_strength~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=4 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
pidstrength.h.nolag.5 <- lm(pid_strength~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=5 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
pidstrength.h.nolag.opt <- lm(pid_strength~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=8.320 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
interest.h.nolag.1 <- lm(interest~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=1 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
interest.h.nolag.2 <- lm(interest~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=2 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
interest.h.nolag.3 <- lm(interest~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=3 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
interest.h.nolag.4 <- lm(interest~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=4 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
interest.h.nolag.5 <- lm(interest~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=5 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
interest.h.nolag.opt <- lm(interest~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=8.320 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
ideoextremity.h.nolag.1 <- lm(ideo_extremity~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=1 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
ideoextremity.h.nolag.2 <- lm(ideo_extremity~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=2 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
ideoextremity.h.nolag.3 <- lm(ideo_extremity~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=3 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
ideoextremity.h.nolag.4 <- lm(ideo_extremity~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=4 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
ideoextremity.h.nolag.5 <- lm(ideo_extremity~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=5 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
ideoextremity.h.nolag.opt <- lm(ideo_extremity~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=8.320 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
age.h.nolag.1 <- lm(age~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=1 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
age.h.nolag.2 <- lm(age~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=2 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
age.h.nolag.3 <- lm(age~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=3 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
age.h.nolag.4 <- lm(age~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=4 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
age.h.nolag.5 <- lm(age~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=5 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
age.h.nolag.opt <- lm(age~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=8.320 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
female.h.nolag.1 <- lm(female~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=1 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
female.h.nolag.2 <- lm(female~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=2 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
female.h.nolag.3 <- lm(female~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=3 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
female.h.nolag.4 <- lm(female~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=4 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
female.h.nolag.5 <- lm(female~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=5 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
female.h.nolag.opt <- lm(female~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=8.320 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
white.h.nolag.1 <- lm(white~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=1 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
white.h.nolag.2 <- lm(white~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=2 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
white.h.nolag.3 <- lm(white~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=3 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
white.h.nolag.4 <- lm(white~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=4 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
white.h.nolag.5 <- lm(white~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=5 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
white.h.nolag.opt <- lm(white~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=8.320 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
educ.h.nolag.1 <- lm(education~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=1 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
educ.h.nolag.2 <- lm(education~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=2 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
educ.h.nolag.3 <- lm(education~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=3 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
educ.h.nolag.4 <- lm(education~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=4 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
educ.h.nolag.5 <- lm(education~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=5 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
educ.h.nolag.opt <- lm(education~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=8.320 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
urbanity.h.nolag.1 <- lm(urbanity~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=1 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
urbanity.h.nolag.2 <- lm(urbanity~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=2 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
urbanity.h.nolag.3 <- lm(urbanity~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=3 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
urbanity.h.nolag.4 <- lm(urbanity~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=4 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
urbanity.h.nolag.5 <- lm(urbanity~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=5 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
urbanity.h.nolag.opt <- lm(urbanity~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=8.320 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
south.h.nolag.1 <- lm(south~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=1 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
south.h.nolag.2 <- lm(south~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=2 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
south.h.nolag.3 <- lm(south~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=3 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
south.h.nolag.4 <- lm(south~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=4 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
south.h.nolag.5 <- lm(south~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=5 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
south.h.nolag.opt <- lm(south~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=8.320 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
income.h.nolag.1 <- lm(income~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=1 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
income.h.nolag.2 <- lm(income~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=2 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
income.h.nolag.3 <- lm(income~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=3 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
income.h.nolag.4 <- lm(income~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=4 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
income.h.nolag.5 <- lm(income~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=5 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
income.h.nolag.opt <- lm(income~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=8.320 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
church.h.nolag.1 <- lm(church~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=1 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
church.h.nolag.2 <- lm(church~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=2 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
church.h.nolag.3 <- lm(church~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=3 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
church.h.nolag.4 <- lm(church~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=4 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
church.h.nolag.5 <- lm(church~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=5 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))
church.h.nolag.opt <- lm(church~winner,data=data.house,subset=abs(data.house$winner.twoparty)<=8.320 & (data.house$year==1978 | data.house$year==1982 | data.house$year==1984 | data.house$year==1986 | data.house$year==1990 | data.house$year==1994 | data.house$year==1996 | data.house$year==1998 | data.house$year==2002 | data.house$year==2004 | data.house$year==2008 | data.house$year==2012 | data.house$year==2016 | data.house$year==2020))

# Table A4, Balance Tests, Senate
pidstrength.s.nolag.1 <- lm(pid_strength~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=1 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
pidstrength.s.nolag.2 <- lm(pid_strength~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=2 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
pidstrength.s.nolag.3 <- lm(pid_strength~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=3 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
pidstrength.s.nolag.4 <- lm(pid_strength~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=4 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
pidstrength.s.nolag.5 <- lm(pid_strength~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=5 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
pidstrength.s.nolag.opt <- lm(pid_strength~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=6.111 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
interest.s.nolag.1 <- lm(interest~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=1 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
interest.s.nolag.2 <- lm(interest~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=2 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
interest.s.nolag.3 <- lm(interest~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=3 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
interest.s.nolag.4 <- lm(interest~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=4 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
interest.s.nolag.5 <- lm(interest~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=5 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
interest.s.nolag.opt <- lm(interest~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=6.111 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
ideoextremity.s.nolag.1 <- lm(ideo_extremity~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=1 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
ideoextremity.s.nolag.2 <- lm(ideo_extremity~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=2 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
ideoextremity.s.nolag.3 <- lm(ideo_extremity~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=3 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
ideoextremity.s.nolag.4 <- lm(ideo_extremity~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=4 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
ideoextremity.s.nolag.5 <- lm(ideo_extremity~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=5 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
ideoextremity.s.nolag.opt <- lm(ideo_extremity~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=6.111 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
age.s.nolag.1 <- lm(age~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=1 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
age.s.nolag.2 <- lm(age~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=2 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
age.s.nolag.3 <- lm(age~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=3 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
age.s.nolag.4 <- lm(age~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=4 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
age.s.nolag.5 <- lm(age~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=5 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
age.s.nolag.opt <- lm(age~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=6.111 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
female.s.nolag.1 <- lm(female~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=1 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
female.s.nolag.2 <- lm(female~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=2 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
female.s.nolag.3 <- lm(female~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=3 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
female.s.nolag.4 <- lm(female~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=4 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
female.s.nolag.5 <- lm(female~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=5 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
female.s.nolag.opt <- lm(female~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=6.111 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
white.s.nolag.1 <- lm(white~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=1 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
white.s.nolag.2 <- lm(white~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=2 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
white.s.nolag.3 <- lm(white~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=3 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
white.s.nolag.4 <- lm(white~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=4 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
white.s.nolag.5 <- lm(white~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=5 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
white.s.nolag.opt <- lm(white~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=6.111 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
educ.s.nolag.1 <- lm(education~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=1 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
educ.s.nolag.2 <- lm(education~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=2 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
educ.s.nolag.3 <- lm(education~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=3 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
educ.s.nolag.4 <- lm(education~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=4 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
educ.s.nolag.5 <- lm(education~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=5 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
educ.s.nolag.opt <- lm(education~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=6.111 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
urbanity.s.nolag.1 <- lm(urbanity~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=1 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
urbanity.s.nolag.2 <- lm(urbanity~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=2 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
urbanity.s.nolag.3 <- lm(urbanity~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=3 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
urbanity.s.nolag.4 <- lm(urbanity~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=4 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
urbanity.s.nolag.5 <- lm(urbanity~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=5 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
urbanity.s.nolag.opt <- lm(urbanity~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=6.111 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
south.s.nolag.1 <- lm(south~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=1 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
south.s.nolag.2 <- lm(south~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=2 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
south.s.nolag.3 <- lm(south~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=3 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
south.s.nolag.4 <- lm(south~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=4 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
south.s.nolag.5 <- lm(south~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=5 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
south.s.nolag.opt <- lm(south~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=6.111 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
income.s.nolag.1 <- lm(income~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=1 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
income.s.nolag.2 <- lm(income~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=2 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
income.s.nolag.3 <- lm(income~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=3 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
income.s.nolag.4 <- lm(income~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=4 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
income.s.nolag.5 <- lm(income~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=5 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
income.s.nolag.opt <- lm(income~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=6.111 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
church.s.nolag.1 <- lm(church~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=1 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
church.s.nolag.2 <- lm(church~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=2 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
church.s.nolag.3 <- lm(church~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=3 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
church.s.nolag.4 <- lm(church~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=4 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
church.s.nolag.5 <- lm(church~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=5 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))
church.s.nolag.opt <- lm(church~winner,data=data.senate,subset=abs(data.senate$winner.twoparty)<=6.111 & (data.senate$year==1978 | data.senate$year==1982 | data.senate$year==1984 | data.senate$year==1986 | data.senate$year==1990 | data.senate$year==1994 | data.senate$year==1996 | data.senate$year==1998 | data.senate$year==2002 | data.senate$year==2004 | data.senate$year==2008 | data.senate$year==2012 | data.senate$year==2016 | data.senate$year==2020))

# Table A5, Balance Tests, Presidential
pidstrength.p.nolag.1 <- lm(pid_strength~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=1 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
pidstrength.p.nolag.2 <- lm(pid_strength~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=2 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
pidstrength.p.nolag.3 <- lm(pid_strength~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=3 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
pidstrength.p.nolag.4 <- lm(pid_strength~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=4 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
pidstrength.p.nolag.5 <- lm(pid_strength~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=5 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
pidstrength.p.nolag.opt <- lm(pid_strength~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=7.667 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
interest.p.nolag.1 <- lm(interest~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=1 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
interest.p.nolag.2 <- lm(interest~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=2 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
interest.p.nolag.3 <- lm(interest~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=3 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
interest.p.nolag.4 <- lm(interest~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=4 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
interest.p.nolag.5 <- lm(interest~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=5 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
interest.p.nolag.opt <- lm(interest~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=7.667 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
ideoextremity.p.nolag.1 <- lm(ideo_extremity~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=1 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
ideoextremity.p.nolag.2 <- lm(ideo_extremity~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=2 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
ideoextremity.p.nolag.3 <- lm(ideo_extremity~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=3 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
ideoextremity.p.nolag.4 <- lm(ideo_extremity~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=4 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
ideoextremity.p.nolag.5 <- lm(ideo_extremity~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=5 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
ideoextremity.p.nolag.opt <- lm(ideo_extremity~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=7.667 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
age.p.nolag.1 <- lm(age~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=1 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
age.p.nolag.2 <- lm(age~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=2 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
age.p.nolag.3 <- lm(age~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=3 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
age.p.nolag.4 <- lm(age~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=4 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
age.p.nolag.5 <- lm(age~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=5 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
age.p.nolag.opt <- lm(age~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=7.667 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
female.p.nolag.1 <- lm(female~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=1 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
female.p.nolag.2 <- lm(female~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=2 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
female.p.nolag.3 <- lm(female~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=3 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
female.p.nolag.4 <- lm(female~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=4 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
female.p.nolag.5 <- lm(female~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=5 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
female.p.nolag.opt <- lm(female~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=7.667 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
white.p.nolag.1 <- lm(white~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=1 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
white.p.nolag.2 <- lm(white~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=2 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
white.p.nolag.3 <- lm(white~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=3 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
white.p.nolag.4 <- lm(white~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=4 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
white.p.nolag.5 <- lm(white~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=5 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
white.p.nolag.opt <- lm(white~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=7.667 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
educ.p.nolag.1 <- lm(education~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=1 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
educ.p.nolag.2 <- lm(education~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=2 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
educ.p.nolag.3 <- lm(education~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=3 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
educ.p.nolag.4 <- lm(education~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=4 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
educ.p.nolag.5 <- lm(education~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=5 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
educ.p.nolag.opt <- lm(education~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=7.667 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
urbanity.p.nolag.1 <- lm(urbanity~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=1 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
urbanity.p.nolag.2 <- lm(urbanity~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=2 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
urbanity.p.nolag.3 <- lm(urbanity~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=3 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
urbanity.p.nolag.4 <- lm(urbanity~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=4 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
urbanity.p.nolag.5 <- lm(urbanity~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=5 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
urbanity.p.nolag.opt <- lm(urbanity~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=7.667 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
south.p.nolag.1 <- lm(south~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=1 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
south.p.nolag.2 <- lm(south~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=2 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
south.p.nolag.3 <- lm(south~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=3 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
south.p.nolag.4 <- lm(south~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=4 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
south.p.nolag.5 <- lm(south~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=5 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
south.p.nolag.opt <- lm(south~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=7.667 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
income.p.nolag.1 <- lm(income~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=1 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
income.p.nolag.2 <- lm(income~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=2 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
income.p.nolag.3 <- lm(income~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=3 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
income.p.nolag.4 <- lm(income~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=4 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
income.p.nolag.5 <- lm(income~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=5 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
income.p.nolag.opt <- lm(income~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=7.667 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
church.p.nolag.1 <- lm(church~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=1 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
church.p.nolag.2 <- lm(church~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=2 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
church.p.nolag.3 <- lm(church~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=3 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
church.p.nolag.4 <- lm(church~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=4 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
church.p.nolag.5 <- lm(church~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=5 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))
church.p.nolag.opt <- lm(church~winner,data=data.president,subset=abs(data.president$winner.twoparty)<=7.667 & (data.president$year==1978 | data.president$year==1982 | data.president$year==1984 | data.president$year==1986 | data.president$year==1990 | data.president$year==1994 | data.president$year==1996 | data.president$year==1998 | data.president$year==2002 | data.president$year==2004 | data.president$year==2008 | data.president$year==2012 | data.president$year==2016 | data.president$year==2020))

# Figure A2: Effect of Eventual Democratic Victory on Prior Democratic Vote Share
## House
house.prior.rdd <- rdrobust::rdrobust(x=house3$dem.twoparty.lag,y=house3$dem.twoparty,covs=house3$year,all=T,vce="hc2")
house.prior.rdd1 <- rdrobust::rdrobust(x=house3$dem.twoparty.lag,y=house3$dem.twoparty,covs=house3$year,all=T,vce="hc2",h=1)
house.prior.rdd2 <- rdrobust::rdrobust(x=house3$dem.twoparty.lag,y=house3$dem.twoparty,covs=house3$year,all=T,vce="hc2",h=2)
house.prior.rdd3 <- rdrobust::rdrobust(x=house3$dem.twoparty.lag,y=house3$dem.twoparty,covs=house3$year,all=T,vce="hc2",h=3)
house.prior.rdd4 <- rdrobust::rdrobust(x=house3$dem.twoparty.lag,y=house3$dem.twoparty,covs=house3$year,all=T,vce="hc2",h=4)
house.prior.rdd5 <- rdrobust::rdrobust(x=house3$dem.twoparty.lag,y=house3$dem.twoparty,covs=house3$year,all=T,vce="hc2",h=5)
## Senate
senate.prior.rdd <- rdrobust::rdrobust(x=senate3$dem.twoparty.lag,y=senate3$dem.twoparty,covs=senate3$year,all=T,vce="hc2")
senate.prior.rdd1 <- rdrobust::rdrobust(x=senate3$dem.twoparty.lag,y=senate3$dem.twoparty,covs=senate3$year,all=T,vce="hc2",h=1)
senate.prior.rdd2 <- rdrobust::rdrobust(x=senate3$dem.twoparty.lag,y=senate3$dem.twoparty,covs=senate3$year,all=T,vce="hc2",h=2)
senate.prior.rdd3 <- rdrobust::rdrobust(x=senate3$dem.twoparty.lag,y=senate3$dem.twoparty,covs=senate3$year,all=T,vce="hc2",h=3)
senate.prior.rdd4 <- rdrobust::rdrobust(x=senate3$dem.twoparty.lag,y=senate3$dem.twoparty,covs=senate3$year,all=T,vce="hc2",h=4)
senate.prior.rdd5 <- rdrobust::rdrobust(x=senate3$dem.twoparty.lag,y=senate3$dem.twoparty,covs=senate3$year,all=T,vce="hc2",h=5)
## President
president.prior.rdd <- rdrobust::rdrobust(x=president3$dem.twoparty.lag,y=president3$dem.twoparty,covs=president3$year,all=T,vce="hc2")
president.prior.rdd1 <- rdrobust::rdrobust(x=president3$dem.twoparty.lag,y=president3$dem.twoparty,covs=president3$year,all=T,vce="hc2",h=1)
president.prior.rdd2 <- rdrobust::rdrobust(x=president3$dem.twoparty.lag,y=president3$dem.twoparty,covs=president3$year,all=T,vce="hc2",h=2)
president.prior.rdd3 <- rdrobust::rdrobust(x=president3$dem.twoparty.lag,y=president3$dem.twoparty,covs=president3$year,all=T,vce="hc2",h=3)
president.prior.rdd4 <- rdrobust::rdrobust(x=president3$dem.twoparty.lag,y=president3$dem.twoparty,covs=president3$year,all=T,vce="hc2",h=4)
president.prior.rdd5 <- rdrobust::rdrobust(x=president3$dem.twoparty.lag,y=president3$dem.twoparty,covs=president3$year,all=T,vce="hc2",h=5)
## Plot
figa2.data <- data.frame(election=factor(c(rep("House",6),rep("Senate",6),rep("President",6)),levels=c("House","Senate","President")),
                         margin=factor(c(rep(c("1%","2%","3%","4%","5%","Optimal"),3)),levels=c("Optimal","5%","4%","3%","2%","1%")),
                         coef=c(house.prior.rdd1$coef[1],house.prior.rdd2$coef[1],house.prior.rdd3$coef[1],house.prior.rdd4$coef[1],house.prior.rdd5$coef[1],house.prior.rdd$coef[1],senate.prior.rdd1$coef[1],senate.prior.rdd2$coef[1],senate.prior.rdd3$coef[1],senate.prior.rdd4$coef[1],senate.prior.rdd5$coef[1],senate.prior.rdd$coef[1],president.prior.rdd1$coef[1],president.prior.rdd2$coef[1],president.prior.rdd3$coef[1],president.prior.rdd4$coef[1],president.prior.rdd5$coef[1],president.prior.rdd$coef[1]),
                         lci=c(house.prior.rdd1$ci[1],house.prior.rdd2$ci[1],house.prior.rdd3$ci[1],house.prior.rdd4$ci[1],house.prior.rdd5$ci[1],house.prior.rdd$ci[1],senate.prior.rdd1$ci[1],senate.prior.rdd2$ci[1],senate.prior.rdd3$ci[1],senate.prior.rdd4$ci[1],senate.prior.rdd5$ci[1],senate.prior.rdd$ci[1],president.prior.rdd1$ci[1],president.prior.rdd2$ci[1],president.prior.rdd3$ci[1],president.prior.rdd4$ci[1],president.prior.rdd5$ci[1],president.prior.rdd$ci[1]),
                         uci=c(house.prior.rdd1$ci[4],house.prior.rdd2$ci[4],house.prior.rdd3$ci[4],house.prior.rdd4$ci[4],house.prior.rdd5$ci[4],house.prior.rdd$ci[4],senate.prior.rdd1$ci[4],senate.prior.rdd2$ci[4],senate.prior.rdd3$ci[4],senate.prior.rdd4$ci[4],senate.prior.rdd5$ci[4],senate.prior.rdd$ci[4],president.prior.rdd1$ci[4],president.prior.rdd2$ci[4],president.prior.rdd3$ci[4],president.prior.rdd4$ci[4],president.prior.rdd5$ci[4],president.prior.rdd$ci[4]))

p.figa2 <- ggplot(figa2.data,aes(x=margin,y=coef)) + geom_point() + theme_classic() + coord_flip() + xlab("Margin") + ylab("Effect") + geom_errorbar(aes(ymin=lci,ymax=uci),lwd=0.5,width=0) + geom_hline(yintercept=0,linetype="dashed",color="red") + facet_wrap(vars(election),ncol=3,nrow=1,scales="free_x")

# Regression Discontinuities Using Z-Scored Measures
## House RDDs
### Affective Polarization (Table A15)
hpl.change1z <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$polarizcses_z,covs=data.house.l$polarizft_z+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2",h=1)
hpl.change2z <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$polarizcses_z,covs=data.house.l$polarizft_z+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2",h=2)
hpl.change3z <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$polarizcses_z,covs=data.house.l$polarizft_z+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2",h=3)
hpl.change4z <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$polarizcses_z,covs=data.house.l$polarizft_z+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2",h=4)
hpl.change5z <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$polarizcses_z,covs=data.house.l$polarizft_z+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2",h=5)
hpl.change.optz <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$polarizcses_z,covs=data.house.l$polarizft_z+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2")
### In-Party Warmth (Table A16)
hil.change1z <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$inpartycses_z,covs=data.house.l$inpartyft_z+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2",h=1)
hil.change2z <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$inpartycses_z,covs=data.house.l$inpartyft_z+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2",h=2)
hil.change3z <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$inpartycses_z,covs=data.house.l$inpartyft_z+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2",h=3)
hil.change4z <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$inpartycses_z,covs=data.house.l$inpartyft_z+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2",h=4)
hil.change5z <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$inpartycses_z,covs=data.house.l$inpartyft_z+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2",h=5)
hil.change.optz <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$inpartycses_z,covs=data.house.l$inpartyft_z+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2")
### Out-Party Warmth (Table A17)
hol.change1z <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$outpartycses_z,covs=data.house.l$outpartyft_z+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2",h=1)
hol.change2z <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$outpartycses_z,covs=data.house.l$outpartyft_z+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2",h=2)
hol.change3z <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$outpartycses_z,covs=data.house.l$outpartyft_z+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2",h=3)
hol.change4z <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$outpartycses_z,covs=data.house.l$outpartyft_z+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2",h=4)
hol.change5z <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$outpartycses_z,covs=data.house.l$outpartyft_z+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2",h=5)
hol.change.optz <- rdrobust::rdrobust(x=data.house.l$winner.twoparty,y=data.house.l$outpartycses_z,covs=data.house.l$outpartyft_z+data.house.l$year+data.house.l$winner.twoparty.lag,all=T,vce="hc2")
## Senate RDDs
### Affective Polarization (Table A18)
spl.change1z <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$polarizcses_z,covs=data.senate.l$polarizft_z+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2",h=1)
spl.change2z <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$polarizcses_z,covs=data.senate.l$polarizft_z+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2",h=2)
spl.change3z <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$polarizcses_z,covs=data.senate.l$polarizft_z+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2",h=3)
spl.change4z <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$polarizcses_z,covs=data.senate.l$polarizft_z+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2",h=4)
spl.change5z <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$polarizcses_z,covs=data.senate.l$polarizft_z+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2",h=5)
spl.change.optz <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$polarizcses_z,covs=data.senate.l$polarizft_z+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2")
### In-Party Warmth (Table A19)
sil.change1z <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$inpartycses_z,covs=data.senate.l$inpartyft_z+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2",h=1)
sil.change2z <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$inpartycses_z,covs=data.senate.l$inpartyft_z+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2",h=2)
sil.change3z <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$inpartycses_z,covs=data.senate.l$inpartyft_z+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2",h=3)
sil.change4z <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$inpartycses_z,covs=data.senate.l$inpartyft_z+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2",h=4)
sil.change5z <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$inpartycses_z,covs=data.senate.l$inpartyft_z+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2",h=5)
sil.change.optz <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$inpartycses_z,covs=data.senate.l$inpartyft_z+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2")
### Out-Party Warmth (Table A20)
sol.change1z <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$outpartycses_z,covs=data.senate.l$outpartyft_z+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2",h=1)
sol.change2z <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$outpartycses_z,covs=data.senate.l$outpartyft_z+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2",h=2)
sol.change3z <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$outpartycses_z,covs=data.senate.l$outpartyft_z+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2",h=3)
sol.change4z <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$outpartycses_z,covs=data.senate.l$outpartyft_z+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2",h=4)
sol.change5z <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$outpartycses_z,covs=data.senate.l$outpartyft_z+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2",h=5)
sol.change.optz <- rdrobust::rdrobust(x=data.senate.l$winner.twoparty,y=data.senate.l$outpartycses_z,covs=data.senate.l$outpartyft_z+data.senate.l$year+data.senate.l$winner.twoparty.lag,all=T,vce="hc2")
## Presidential RDDs
### Affective Polarization (Table A21)
ppl.change1z <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$polarizcses_z,covs=data.president.l$polarizft_z+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2",h=1)
ppl.change2z <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$polarizcses_z,covs=data.president.l$polarizft_z+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2",h=2)
ppl.change3z <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$polarizcses_z,covs=data.president.l$polarizft_z+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2",h=3)
ppl.change4z <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$polarizcses_z,covs=data.president.l$polarizft_z+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2",h=4)
ppl.change5z <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$polarizcses_z,covs=data.president.l$polarizft_z+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2",h=5)
ppl.change.optz <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$polarizcses_z,covs=data.president.l$polarizft_z+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2")
### In-Party Warmth (Table A22)
pil.change1z <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$inpartycses_z,covs=data.president.l$inpartyft_z+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2",h=1)
pil.change2z <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$inpartycses_z,covs=data.president.l$inpartyft_z+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2",h=2)
pil.change3z <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$inpartycses_z,covs=data.president.l$inpartyft_z+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2",h=3)
pil.change4z <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$inpartycses_z,covs=data.president.l$inpartyft_z+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2",h=4)
pil.change5z <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$inpartycses_z,covs=data.president.l$inpartyft_z+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2",h=5)
pil.change.optz <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$inpartycses_z,covs=data.president.l$inpartyft_z+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2")
### Out-Party Warmth (Table A23)
pol.change1z <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$outpartycses_z,covs=data.president.l$outpartyft_z+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2",h=1)
pol.change2z <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$outpartycses_z,covs=data.president.l$outpartyft_z+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2",h=2)
pol.change3z <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$outpartycses_z,covs=data.president.l$outpartyft_z+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2",h=3)
pol.change4z <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$outpartycses_z,covs=data.president.l$outpartyft_z+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2",h=4)
pol.change5z <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$outpartycses_z,covs=data.president.l$outpartyft_z+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2",h=5)
pol.change.optz <- rdrobust::rdrobust(x=data.president.l$winner.twoparty,y=data.president.l$outpartycses_z,covs=data.president.l$outpartyft_z+data.president.l$year+data.president.l$winner.twoparty.lag,all=T,vce="hc2")
